package Work2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Pro8 {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		List <String> list = new ArrayList();
		while(in.hasNext()){
			String str = in.next();
			if(str.equals("!!end!!"))break;
			list.add(str);
		}

		list.add("end");
		list.add(0, "begin");
		System.out.println(list);
		String strtemp = in.next();
		isInlist(strtemp,list);
		isInlistback(strtemp,list);
		System.out.println(list.get(0));
		list.remove(0);
		System.out.println(list);
		list.set(1,in.next());
		System.out.println(list);
		String str = in.next();
		List <String> list2 = new ArrayList();
		int flag = 1;
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).contains(str)) {
				list2.add(list.get(i));
				if (flag == 1){
					list.remove(i);
					flag = 0;
					i--;
				}
			}
		}
		System.out.println(list2);
		System.out.println(list);
		list.clear();
		System.out.printf(list+" "+list.size()+","+list.isEmpty());
	}

	private static int isInlistback(String strtemp, List<String> list) {	
		for(int i = list.size() -1; i>=0;i--){
			String tem = list.get(i);
			if(tem.equals(strtemp)){
				System.out.println(i);
				return i;
			}
		}
		System.out.println("-1");
		return -1;
	}

	private static int isInlist(String strtemp, List<String> list) {
		for(int i = 0; i<list.size() ;i++){
			String tem = list.get(i);
			if(tem.equals(strtemp))
			{  System.out.println("true");
				System.out.println(i);
				return i;
			}
		}
		System.out.println("false");
		System.out.println("-1");
		return -1;
	}
}
